package com.huluobo.a2109mvpimdemo.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.disklrucache.DiskLruCache;
import com.huluobo.a2109mvpimdemo.R;
import com.hyphenate.chat.EMClient;

/**
 * 动画:
 * 补间动画:演示效果,平移(translate),缩放(scale),旋转(rotate),渐变(alpha),只需要设置起始值和终点值即可,系统会自动补充其中的动画
 * 帧动画:一帧一帧的动画,需要大量的图片资源才好看,一般不推荐使用,因为大量图片会影响包的体积
 * 属性动画:与补间动画很像,但是实际改变了imageView或者View的属性,补间动画的缺点在于动画过后,实际的view位置没有改变
 * 但是属性动画会实际改变view的值
 * 1.ValueAnimator 和ObjectAnimator
 */
public class DemoActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button tran, rotate, scale, alpha, set, valueAnimatorBtn, objectAnimatorBtn, animatorSetBtn,
            InterpolatorBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        imageView = findViewById(R.id.imageview);
        tran = findViewById(R.id.tran);
        rotate = findViewById(R.id.rotate);
        scale = findViewById(R.id.scale);
        alpha = findViewById(R.id.alpha);
        set = findViewById(R.id.set);
        valueAnimatorBtn = findViewById(R.id.valueAnimator);
        objectAnimatorBtn = findViewById(R.id.objectAnimator);
        animatorSetBtn = findViewById(R.id.animatorSet);
        InterpolatorBtn = findViewById(R.id.Interpolator);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
        tran.setOnClickListener(v -> imageView.startAnimation(animation));

        Animation animation_rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        rotate.setOnClickListener(v -> imageView.startAnimation(animation_rotate));

        Animation animation_scale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        animation_scale.setRepeatMode(Animation.REVERSE);
        animation_scale.setRepeatCount(Animation.INFINITE);
        scale.setOnClickListener(v -> {
            imageView.startAnimation(animation_scale);
        });

        Animation animation_alpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        alpha.setOnClickListener(v -> imageView.startAnimation(animation_alpha));

        Animation animation_set = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        animation_set.setRepeatCount(-1);
        animation_set.setRepeatMode(Animation.REVERSE);
        set.setOnClickListener(v -> {
            imageView.startAnimation(animation_set);
        });

        valueAnimatorBtn.setOnClickListener(v -> {
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0.1f, 1f, 0.5f, 1f);
            valueAnimator.setDuration(5000);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//设置执行重复次数(infinite)代表无限
            valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//设置执行重复模式reverse代表翻转的意思
            valueAnimator.addUpdateListener(animation1 -> {
                float currentValue = (float) animation1.getAnimatedValue();
                Log.i("TagA", "currentValue:" + currentValue);
                imageView.setAlpha(currentValue);//改变view的透明度
                imageView.setScaleX(currentValue);
                imageView.setScaleY(currentValue);
            });
            valueAnimator.start();
        });

        //alpha 渐变
        //scaleX X轴的缩放
        //scaleY Y轴的缩放
        //translateX X轴的平移
        //translateY Y轴的平移
        //rotation 旋转
        objectAnimatorBtn.setOnClickListener(v -> {
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f, 1f, 0.5f, 1f);
            objectAnimator.setDuration(1500);
            objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
            objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
            objectAnimator.start();
        });

        animatorSetBtn.setOnClickListener(v -> {
            ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360);
            ObjectAnimator translateX = ObjectAnimator.ofFloat(imageView, "translationX", -100, 100);
            ObjectAnimator translateY = ObjectAnimator.ofFloat(imageView, "translationY", -100, 100);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1, 2);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1, 2);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 0, 1);

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(rotate)//要执行的效果,必须要有个play
                    .with(alpha)//与play同时执行
                    .with(scaleY)
                    .after(scaleX)//执行完,再执行play
                    .after(1000)//将play延迟1秒后执行,也就是执行完1秒,再执行play
                    .before(translateX)//执行完play,再执行
                    .before(translateY);

            animatorSet.setDuration(15000);
            animatorSet.start();
        });

        InterpolatorBtn.setOnClickListener(v -> {
            ObjectAnimator translateY = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360);
            //加入插值器
//            translateY.setInterpolator(new AccelerateInterpolator());//加速度插值器 开始慢,中间开始加速
//            translateY.setInterpolator(new DecelerateInterpolator());//减速插值器,开始快,中间开始减速
//            translateY.setInterpolator(new AccelerateDecelerateInterpolator());//先加速,后减速,开始慢 中间快,结束慢
//            translateY.setInterpolator(new AnticipateInterpolator());//反向加速,先后退一段距离,然后再加速
            translateY.setInterpolator(new BounceInterpolator());//探求落地效果
//            translateY.setInterpolator(new CycleInterpolator(2));//循环插值器
//            translateY.setInterpolator(new LinearInterpolator());//线性插值器,匀速运动,默认
//            translateY.setInterpolator(new OvershootInterpolator());//超越插值器,先反向加速,超过终点一段距离,再返回终点

            translateY.setDuration(1000);
            translateY.setRepeatCount(-1);
            translateY.setRepeatMode(ValueAnimator.RESTART);
            translateY.start();
        });
    }
}